package com.ding.easycontroller.file.impl;

import com.ding.easycontroller.file.ScriptFilePathHolder;
import com.ding.easycontroller.file.ServiceFile;
import com.ding.easycontroller.utils.ElementUtils;
import com.ding.easycontroller.work.Service;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

@Component
public class ServiceFileImpl implements ServiceFile {

    @Autowired
    private XMLOutputter docWriter;

    private File root;


    ServiceFileImpl(ScriptFilePathHolder holder) {
        File parent = holder.getRoot();
        File file = new File(parent, "service");
        if (!file.exists() && !file.mkdirs()) {
            throw new RuntimeException("路径创建失败:" + file.getAbsolutePath());
        }
        this.root = file;
    }

    private Document service2Document(Service service) {
        Document document = new Document();

        Element root = new Element("service");

        //基础恢复
        ScriptFile.scriptToElement(service, root);

        //服务名称
        Element name = new Element("name");
        name.setText(service.getName());
        root.getChildren().add(name);

        document.setRootElement(root);

        return document;
    }

    @Override
    public void add(Service service) {

        try {
            Document document = service2Document(service);
            String strFromXml = docWriter.outputString(document);
            FileWriter writer = new FileWriter(new File(this.root, service.getId() + ".service"));
            writer.write(strFromXml);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeById(String id) {
        if (!new File(root, id + ".service").delete()) {
            throw new RuntimeException("service移除失败:" + id);
        }
    }

    @Override
    public List<Service> listAll() {
        List<Service> all = new ArrayList<>();
        File[] files = root.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile() && file.getName().endsWith(".service")) {
                    Element element = ElementUtils.readElement(file);
                    if (element != null && element.getName().equals("service")) {
                        Service service = new Service();
                        ScriptFile.elementToScript(element, service);
                        service.setName(element.getChild("name").getText());
                        all.add(service);
                    }
                }
            }
        }
        return all;
    }

    @Override
    public void update(Service service) {
        try {
            Document document = service2Document(service);
            String strFromXml = docWriter.outputString(document);
            FileWriter writer = new FileWriter(new File(this.root, service.getId() + ".service"));
            writer.write(strFromXml);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
